<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess" ref="tableRef">
      <template #toolbar v-if="!teamGroupsSelete">
        <a-button type="primary" @click="handleCreate"  v-if="hasPermission(['bpm:process-instance:create'])"> 发起流程 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '详情',
              icon: 'clarity:note-edit-line',
              onClick: handleDeatil.bind(null, record),
              ifShow: !teamGroupsSelete && hasPermission(['bpm:form:info']),
            },
            {
              label: '取消',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              ifShow: record.result === 1 && hasPermission(['bpm:process-instance:cancel']),
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess" :type="type" />
    <Modal v-model:visible="visible" title="取消流程" @ok="handleOk">
      <div style="padding: 16px">
        <p><span style="color: red;">*</span>请输入取消原因</p>
        <a-input v-model:value="cancleData.reason" placeholder="" />
      </div>
    </Modal>
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, toRefs, unref, ref } from 'vue';
  import { Modal, message } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction, TableActionType } from '/@/components/Table';
  import { getProcessList, createProcess, deleteProcess } from '/@/api/backs/workFlow/process';
  import { useDrawer } from '/@/components/Drawer';
  import Drawer from './components/Drawer.vue';
  import { columns, searchFormSchema } from './components/tableData';
  import { useGo } from '/@/hooks/web/usePage';
  import { usePermission } from '/@/hooks/web/usePermission';
  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, Drawer, TableAction, Modal },
    props: {
      teamGroupsSelete: {
        type: Boolean,
        default: false,
      },
    },
    setup(props) {
      const { hasPermission } = usePermission();
      const go = useGo();
      const tableRef = ref<Nullable<TableActionType>>(null);
      const getTableAction = () => {
        const tableAction = unref(tableRef);
        if (!tableAction) {
          throw new Error('tableAction is null');
        }
        return tableAction;
      };
      const [registerDrawer, { openDrawer, closeDrawer }] = useDrawer();
      const [registerTable, { reload, getSelectRows, setSelectedRowKeys }] = useTable({
        title: '我的流程',
        api: getProcessList,
        columns,
        formConfig: {
          labelWidth: 100,
          schemas: searchFormSchema,
        },
        rowKey: 'id',
        isTreeTable: false,
        pagination: true,
        striped: false,
        useSearchForm: !props.teamGroupsSelete,
        showTableSetting: !props.teamGroupsSelete,
        bordered: true,
        showIndexColumn: false,
        canResize: false,
        rowSelection: props.teamGroupsSelete,
        beforeFetch: (res) => {
          if (res.createTime && res.createTime.length) {
            res.startTime = res.createTime[0];
            res.endTime = res.createTime[1];
            delete res.createTime;
          }
        },
        actionColumn: {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      const getSelectRowList = () => {
        return getTableAction().getSelectRows();
      };
      const setSelectedRowKeyList = (arr) => {
        setSelectedRowKeys(arr);
      };
      const data: any = reactive({
        visible: false,
        type: 'add',
        cancleData: {
          id: null,
          reason: '',
        },
      });
      //编辑
      const handleDeatilSuccess = async (formData) => {};
      //新增
      const handleAddSuccess = async (formData) => {
        let params = { ...formData };
        try {
          await createProcess(params);
          closeDrawer();
          reload();
        } catch (error) {}
      };
      function handleCreate() {
        go('/workFlowManage/task/create');
      }

      function handleDeatil(record: Recordable) {
        go('/workFlowManage/task/detail/' + record.id);
      }

      //删除
      const handleDelete = async (record: Recordable) => {
        data.visible = true;
        data.cancleData.id = record.id;
        data.cancleData.reason = '';
      };
      const handleOk = async () => {
        try {
          await deleteProcess(data.cancleData);
          data.visible = false;
          message.success('取消成功！');
          reload();
        } catch (error) {}
      };
      function handleSuccess(formData) {
        switch (data.type) {
          case 'add':
            handleAddSuccess(formData);
            break;
          case 'edit':
            handleDeatilSuccess(formData);
            break;
        }
      }

      function onFetchSuccess() {}

      return {
        ...toRefs(data),
        tableRef,
        registerTable,
        registerDrawer,
        handleCreate,
        handleDeatil,
        handleDelete,
        handleSuccess,
        onFetchSuccess,
        getSelectRowList,
        setSelectedRowKeyList,
        handleOk,
        hasPermission,
      };
    },
  });
</script>
